Cornell University ECE4760
USB Host HID

Pi Pico RP2040

USB mouse and keyboard
The USB HID demo code on the Pico C-SDK site works as expected to produce mouse reports and keyboard reports in the serial window. As a first step in figuring how to use the library, I ported the demo application to our environment. This includes Protothreads and a VGA system running on RP2040. The result is shown below. The example decodes and uses left (draw), middle (pick color), and right (place text) mouse button clicks, controls drawing with the mouse position, and displays text from the USB keyboard.

The USB task, tuh_task(), runs in a thread every 8 mSec, which should be fast enough to get mouse events, except perhaps for gaming. It seems that the USB initializer, tuh_init(BOARD_TUH_RHPORT);,  takes some time, so main stalls for 500 mSec after calling tuh_init and before the threader starts. There are three threads running: the USB thread, the usual LED blinky, and the VGA graphics thread. The USB thread runs on core 0 and just calls the HID task and yields for 8 mSec. The VGA thread runs on core 1 about 30 times/sec to animate the GUI. The GUI event loop in the VGA thread checks for mouse motion, mouse buttons and keypresses, then does whatever action is appropriate based on the mouse position and buttons.

The VGA graphics library was modified to add the ability to read back the color of a pixel, for color choice.
The hid_app was modified to grab the mouse and keyboard reports and put them in global variables.

Code, Project ZIP



Copyright Cornell University March 1, 2024